package com.chatTools.test;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @author： pang-yun
 * @date： 2021-09-18 15:41
 * 功能目的 ：用于  connect timeout millis
 */

/**
 * 客户端通过 bootstrap.option 方法来给 socketchannel 配置参数
 * <p>
 * 服务器端  new ServerSocketChannel.option()  是给 ServerSocketChannel 配置参数
 * new ServerSocketChannel.childOption() 是给 socketChannel  来配置参数
 */


@Slf4j
public class ConnectionTimeout {
    public static void main(String[] args) {
        NioEventLoopGroup group = new NioEventLoopGroup();

        try {
            ChannelFuture channelFuture = new Bootstrap()
                    .group(group)
                    // 配置参数  如果 300ms 不能2连接成功
                    .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 300)
                    .channel(NioSocketChannel.class)
                    .handler(new LoggingHandler(LogLevel.INFO)).connect("127.0.0.1", 9999);

            channelFuture.sync().channel().closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
            log.info("time out.....");
        } finally {
            group.shutdownGracefully();
        }
    }
}
